<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <title>网站管理</title>
    <link rel="stylesheet" href="/static/pengpai/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="category-table" lay-filter="category-table"></table>
    </div>
</div>

<script type="text/html" id="category-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" sec:authorize="hasAuthority('web:category:create')">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" sec:authorize="hasAuthority('web:category:delete')">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="createHeader" sec:authorize="hasAuthority('web:category:create')">
        <i class="layui-icon layui-icon-delete"></i>
        生成header
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠
    </button>
</script>

<script type="text/html" id="category-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" name="编辑" sec:authorize="hasAuthority('web:category:update')"><i class="layui-icon layui-icon-edit">编辑</i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" sec:authorize="hasAuthority('web:category:delete')"><i class="layui-icon layui-icon-delete">删除</i></button>
</script>

<script type="text/html" id="category-type">
    {{#if (d.categoryModel == '1') { }}
    <span>封面</span>
    {{# }else if(d.categoryModel == '2'){ }}
    <span>列表</span>
    {{# }else if(d.categoryModel == '3'){ }}
    <span>外部链接</span>
    {{# } }}
</script>

<script type="text/html" id="category-isEnabled">
    {{#if (d.isShow == 1) { }}
    <span class="layui-badge layui-bg-green">Y</span>
    {{# }else if(d.isShow == 0){ }}
    <span class="layui-badge layui-bg-red">N</span>
    {{# } }}
</script>

<script src="/static/pengpai/component/layui/layui.js"></script>
<script src="/static/pengpai/component/pear/pear.js"></script>
<script>
    layui.use(['table','jquery','treetable', 'popup'],function () {
        let table = layui.table;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        let popup = layui.popup;

        window.render = function(){
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'categoryId',
                treePidName: 'parentId',
                skin:'line',
                treeDefaultClose: false,
                toolbar:'#category-toolbar',
                elem: '#category-table',
                url: '/webCategory/list',
                page: false,
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'categoryName', minWidth: 200, title: '栏目名称'},
                        {field: 'isShow', minWidth: 200, title: '状态',templet: '#category-isEnabled',},
                        {
                            field: 'categoryModel', width: 150, align: 'center', templet: function (d) {
                                if (d.categoryModel == '1') {
                                    return '<span class="layui-badge layui-bg-gray">封面</span>';
                                }
                                if (d.categoryModel == '2') {
                                    return '<span class="layui-badge layui-bg-blue">列表</span>';
                                } else {
                                    return '<span class="layui-badge layui-bg-green">外部链接</span>';
                                }
                            }, title: '栏目类型'
                        },
                        {field: 'visitUrl', title: '访问地址'},
                        {field: 'sort', title: '排序'},
                        {title: '操作',templet: '#category-bar', width: 300, align: 'center'}
                    ]
                ]
            });
        }

        render();

        table.on('tool(category-table)',function(obj){
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })


        table.on('toolbar(category-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            } else if(obj.event === 'batchRemove'){
                window.batchRemove(obj);
            }  else if(obj.event === 'expandAll'){
                treetable.expandAll("#category-table");
            } else if(obj.event === 'foldAll'){
                treetable.foldAll("#category-table");
            } else if(obj.event === 'createHeader'){
                window.createHeader(obj);
            }
        });

        window.add = function(){
            var index = layer.open({
                title: '添加栏目',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/webCategory/add' + "?pid=1",
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }

        window.edit = function(obj){
            layer.open({
                type: 2,
                title: '编辑栏目',
                shade: 0.1,
                area: ['100%', '100%'],
                content: '/webCategory/edit?id='+obj.data.id,
                async: false,
            });
        }
        window.remove = function(obj){
            layer.confirm('确定要删除该栏目', function(index){
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/webCategory/del",
                    dataType:'json',
                    type:'post',
                    data: {"ids":obj.data['categoryId']},
                    success:function(result){
                        layer.close(loading);
                        if(result.code == 200){
                            popup.success(result.msg, function () {
                                obj.del();
                            });
                        }else{
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.batchRemove = function(obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据");
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].categoryId + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些栏目', {
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url:"/webCategory/del",
                    dataType: 'json',
                    type: 'post',
                    data: {"ids":ids},
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            popup.success(result.msg, function () {
                                treetable.reload('#category-table');
                                // obj.del();
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.createHeader = function(obj) {
            layer.confirm('确定生成新的Header页面', {
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url:"/createHeader",
                    dataType: 'json',
                    type: 'get',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            popup.success(result.msg, function () {
                                treetable.reload('#category-table');
                                // obj.del();
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

    })
</script>
</body>
</html>